<?php
// +----------------------------------------------------------------------
// |     【CustomerMain】Controller 控制器
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\admin\controller\customer;

use think\facade\Request;
use app\common\service\CustomerMain as S;
use app\common\model\CustomerMain as M;
use app\common\model\CustomerGrade as GRD;
use app\common\model\CustomerSample as SAP;
use app\common\model\DealMain as DM;
use QL\QueryList;

class Main extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];

	//初始化
	public function initialize(){
		parent::initialize();
		
		$this->assign('controller_title','客户管理');
	}

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList([['uuid','=',$this->uuid]]));
        }
        return $this->fetch();
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
			//追加uuid
			$data=array_merge(Request::post(),['uuid'=>$this->uuid]);
            return $this->getJson(S::goAdd($data));
        }
        return $this->fetch('edit',[
			'action_up'=>false,
			'grade'=>GRD::listData(['uuid'=>$this->uuid]),
		]);
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(),['id'=>$id,'uuid'=>$this->uuid]));
        }
		
		$model=M::info(['uuid'=>$this->uuid,'id'=>$id]);
		if(!$model) $this->getJson(102);
        
		return $this->fetch('',[
			'action_up'=>true,
			'grade'=>GRD::listData(),
			'model' => $model,
		]);
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post(),['id'=>$id,'uuid'=>$this->uuid]));
    }

    // 删除
    public function remove($id){
		$where=['cusid'=>$id,'uuid'=>$this->uuid];
		$count1=DM::where($where)->count();
		$count2=SAP::where($where)->count();
		if($count1>0 || $count2>0){
			return $this->getJson(['code'=>201,'msg'=>'该客户有购买记录，无法删除！']);
		}else{
			return $this->getJson(S::goRemove(['id'=>$id,'uuid'=>$this->uuid]));
		}
        
    }

    // 批量删除
    public function batchRemove($ids){
		$where=[
			['cusid','in',$ids],
			['uuid','=',$this->uuid],
		];
		$count1=DM::where($where)->count();
		$count2=SAP::where($where)->count();
		if($count1>0 || $count2>0){
			return $this->getJson(['code'=>201,'msg'=>'该客户有购买记录，无法删除！']);
		}else{
			return $this->getJson(S::goBatchRemove([
				['id','in',Request::post('ids')],
				['uuid','=',$this->uuid],
			]));
		}
    }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle([['uuid','=',$this->uuid]]));
        }
        return $this->fetch();
    }
	
    // 设置某个值
    public function setValue(){
        if (Request::isAjax()) {
            return $this->getJson(S::setFieldValue(Request::post(),['id'=>I('post.id'),'uuid'=>$this->uuid]));
        }
        return $this->fetch();
    }
	
	//查询客户信息
	public function findName(){
		if(Request::isAjax()){
			$keyword=I('post.key','');
			if($keyword!=''){
				$where=[
					['uuid','=',$this->uuid],
					["name|company","like", "%" . $keyword . "%"],
				];
				$list=M::listData($where);
			}else{
				$list=M::listData(['uuid'=>$this->uuid],'id desc',1,15);
			}
			$data=[
				'code'=>0,
				'data'=>$list,
			];
			return $this->getJson($data);
		}else{
			return toFalse(404);
		}
	}
	
	//更新地址
	public function upAddress(){
		die;
		$cus=M::listData("id=1");
		foreach($cus as $c){
			$address=trim($c['address']);
			if($address!=""){
				$res=$this->smartAddress($address);
				echo "[".$c['id']."]  ".$address;
				$data=[
					"province"=>$res["province"]??'',
					"city"=>$res["city"]??'',
					"area"=>$res["county"]??'',
					"address"=>$res["address"].$res['name'],
				];
				M::where(["id"=>$c["id"]])->save($data);
				pp($data,false);
			}
		}
	}
	
	//自动识别地址
	protected function smartAddress($address){
		$url="https://wangzc.wang/smAddress";
		$data=[
			'address'=>$address
		];
		$result=QueryList::postJson($url,$data)->gethtml();
		p($result,false);
		$result=json_decode($result,true);
		return $result;
	}
}
